package com.anhtuan.reminder.ultils;

import android.annotation.SuppressLint;
import android.app.Activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.anhtuan.reminder.R;
import com.anhtuan.reminder.listelement.AlertListElement;
import com.anhtuan.reminder.listelement.BaseListElement;
import com.anhtuan.reminder.listelement.IncomingCallListElement;
import com.anhtuan.reminder.listelement.OutgoingCallListElement;
import com.anhtuan.reminder.listelement.SendSMSListElement;
import com.anhtuan.reminder.providers.TaskItem;

public class Ultils {
	@SuppressWarnings("finally")
	@SuppressLint("SimpleDateFormat")
	public static Date convertToDate(String string, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date = new Date();
		
		try {
			date = format.parse(string);
		} catch (ParseException e) {
			e.printStackTrace();
		} finally {
			return date;
		}
	}
	
	// Convert task item to base list element
	public static BaseListElement convertToBaseListElement(Activity activity, TaskItem taskItem, int requestCode) {
		BaseListElement element = null;

		switch (taskItem.getAction()) {

		case ActionItem.ALERT:

			element = new AlertListElement(activity,
					requestCode);
			
			if (taskItem.getRepeat() == RepeatItem.REPEAT_WEEKLY) {
				element.setDescription(String.format(
						activity.getResources().getString(R.string.alert_description_format_text),
												taskItem.getSecContent(), 
												taskItem.getFirstContent() + " " +
												String.format(
														activity.getResources().getString(R.string.repeat_weekly_text_format), 
														activity.getResources().getStringArray(R.array.dayofweek)[taskItem.getDay() - 1])));
				
			} else if (taskItem.getRepeat() == RepeatItem.REPEAT_DAILY){
				
				element.setDescription(String.format(
						activity.getResources().getString(R.string.alert_description_format_text),
						taskItem.getSecContent(), 
						taskItem.getFirstContent() + " " +
						String.format(
								activity.getResources().getString(R.string.repeat_weekly_text_format), "day")));
				
			} else {
				
				element.setDescription(String.format(
						activity.getResources().getString(R.string.alert_description_format_text),
						taskItem.getSecContent(),
						taskItem.getFirstContent())); 

			}
			
			element.setTag(taskItem.getId());

			break;

		case ActionItem.SEND_SMS:
			
			element = new SendSMSListElement(activity,
					requestCode);
			
			if (taskItem.getRepeat() == RepeatItem.REPEAT_WEEKLY) {
				
				element.setText(String.format(
						activity.getResources().getString(R.string.sendsms_text),
						taskItem.getSecContent()) + " " + 
						String.format(
								activity.getResources().getString(R.string.repeat_weekly_text_format), 
								activity.getResources().getStringArray(R.array.dayofweek)[taskItem.getDay() - 1]));
				
			} else if (taskItem.getRepeat() == RepeatItem.REPEAT_DAILY) {
				
				element.setText(String.format(
						activity.getResources().getString(R.string.sendsms_text),
						taskItem.getSecContent()) + " " + 
						String.format(
								activity.getResources().getString(R.string.repeat_weekly_text_format), "day"));
				
			} else {
				
				element.setText(String.format(
						activity.getResources().getString(R.string.sendsms_text),
						taskItem.getSecContent()));
				
			}
			
			element.setDescription(taskItem.getThirdContent());
			element.setTag(taskItem.getId());

			break;

		case ActionItem.OUTGOING_CALL:

			element = new OutgoingCallListElement(activity,
					requestCode);
			element.setText(String.format(
					activity.getResources().getString(R.string.call_text),
					taskItem.getSecContent()));
			
			if (taskItem.getRepeat() == RepeatItem.REPEAT_WEEKLY) {
				
				element.setDescription(String.format(
						activity.getResources().getString(R.string.call_description),
												taskItem.getFirstContent(),
												String.format(activity.getResources().getString(R.string.repeat_weekly_text_format),
														activity.getResources().getStringArray(R.array.dayofweek)[taskItem.getDay() - 1])));
				
			} else if (taskItem.getRepeat() == RepeatItem.REPEAT_DAILY) {

				element.setDescription(String.format(
						activity.getResources().getString(R.string.call_description),
						taskItem.getFirstContent(),
						String.format(activity.getResources().getString(R.string.repeat_weekly_text_format),
								"day")));
				
			} else {
				
				element.setDescription(String.format(
						activity.getResources().getString(R.string.call_description),
						taskItem.getFirstContent(), ""));
				
			}
			
			element.setTag(taskItem.getId());
			
			break;

		case ActionItem.INCOMING_CALL:

			element = new IncomingCallListElement(activity,
					requestCode);
			element.setText(String.format(activity.getResources().getString(R.string.callme_format_text), taskItem.getSecContent()));
			
			if (taskItem.getRepeat() == RepeatItem.REPEAT_WEEKLY) {
			
			element.setDescription(String.format(
					activity.getResources().getString(R.string.call_description),
											taskItem.getFirstContent(),
											String.format(activity.getResources().getString(R.string.repeat_weekly_text_format),
													activity.getResources().getStringArray(R.array.dayofweek)[taskItem.getDay() - 1])));
			} else if (taskItem.getRepeat() == RepeatItem.REPEAT_DAILY) {

					element.setDescription(String.format(
							activity.getResources().getString(R.string.call_description),
							taskItem.getFirstContent(),
							String.format(activity.getResources().getString(R.string.repeat_weekly_text_format),
									"day")));
				
			} else {
				
				element.setDescription(String.format(
						activity.getResources().getString(R.string.call_description),
						taskItem.getFirstContent(), ""));
			}
			
			element.setTag(taskItem.getId());

			break;

		default:
			break;
		}
		
		return element;
	}
}
